package rz.time;

import java.util.Calendar;
import java.util.Date;


public class DateUtil {

	/**
	 * 取季度时间范围
	 * @param date 
	 * @return
	 */
	public static Date[] getQuarterScope(Date date) {
		Date[] dateScope = new Date[2];
		
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int quarter = (month/3)*3;
		
		calendar.set(year, quarter, 1, 0, 0, 0);
		dateScope[0] = calendar.getTime();
		
		calendar.set(year, quarter + 3, 1, 0, 0, 0);
		dateScope[1] = calendar.getTime();
		
		return dateScope;
	}
	/**
	 * 取季度时间范围
	 * @param date 
	 * @return
	 */
	public static Date[] getQuarterScope(int year, int quarter) {
		Date[] dateScope = new Date[2];
		
		Calendar calendar = Calendar.getInstance();
		
		int month = (quarter-1)*3;
		
		calendar.set(year, month, 1, 0, 0, 0);
		dateScope[0] = calendar.getTime();
		
		calendar.set(year, month + 3, 1, 0, 0, 0);
		dateScope[1] = calendar.getTime();
		
		return dateScope;
	}
	
	/**
	 * 取半年时间范围
	 * @param date 
	 * @param halfYear
	 * @return
	 */
	public static Date[] getHalfYearScope(int year, int halfYear) {
		Date[] dateScope = new Date[2];
		
		Calendar calendar = Calendar.getInstance();
		
		int month = (halfYear-1)*6;
		
		calendar.set(year, month, 1, 0, 0, 0);
		dateScope[0] = calendar.getTime();
		
		calendar.set(year, month + 6, 1, 0, 0, 0);
		dateScope[1] = calendar.getTime();
		
		return dateScope;
	}
	
	
	/**
	 * 取年份时间范围
	 * @param date 
	 * @return
	 */
	public static Date[] getYearScope(int year) {
		Date[] dateScope = new Date[2];
		
		Calendar calendar = Calendar.getInstance();
		
		calendar.set(year, 0, 1, 0, 0, 0);
		dateScope[0] = calendar.getTime();
		
		calendar.set(year + 1, 0, 1, 0, 0, 0);
		dateScope[1] = calendar.getTime();
		
		return dateScope;
	}
	
	public static Date firstDayOfMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		calendar.clear();
		calendar.set(year, month, 0, 0, 0, 0);
		return calendar.getTime();
	}
	
	public static Date cycle(Date startDate, int cycle) {
		Calendar now = Calendar.getInstance();//当前时间
		
		Calendar start = Calendar.getInstance();
		start.setTime(startDate);
		int n = 0;//相差多少个月
		while (now.after(start)) {//时间相加到当前月月底才会停止
			n++;
			start.add(Calendar.MONTH, 1);
		}
		start.add(Calendar.MONTH, cycle - (n%cycle==0? cycle : n%cycle));
		start.set(Calendar.DAY_OF_MONTH, start.getMaximum(Calendar.DAY_OF_MONTH));
		return start.getTime();
	}
	
	/*
	 * 返回当前年月
	 */
	public static int[] currentMonth() {
		int[] now = new int[2];
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH) + 1;
		now[0] = year;
		now[1] = month;
		return now;
	}
	/*
	 * 循环最近一次截止日期
	 */
	public static int[] cycle(int startYear, int startMonth, int cycle) {
		int[] newTime = new int[2];
		
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH) + 1;
		int period = (year - startYear)*12 + (month - startMonth);
		newTime[1] = month + (cycle - period%cycle);
		newTime[0] = year; 
		if (newTime[1] > 12) {
			newTime[0] = year + 1;
			newTime[1] = newTime[0]%12;
		}
		
		return newTime;
	}
	
	public static void main(String[] args) {
		Date[] dateScope = getQuarterScope(new Date());
		System.out.println(dateScope[0] + ", " + dateScope[1]);
		
		
		dateScope = getYearScope(2010);
		System.out.println(dateScope[0] + ", " + dateScope[1]);
		
		Calendar start = Calendar.getInstance();
		start.set(2011, 0, 0, 0, 0, 0);
		System.out.println(start.getTime() + ", " + cycle(start.getTime(), 3));
	}
}
